package sk.stuba.fiit.clientApp.gui.handler;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;

import sk.stuba.fiit.clientApp.cvo.FileDetailCVO;
import sk.stuba.fiit.clientApp.cvo.VerifyPanelDataCVO;
import sk.stuba.fiit.clientApp.gui.model.FileTableModel;
import sk.stuba.fiit.clientApp.gui.panel.BasicPanel;
import sk.stuba.fiit.clientApp.singeltons.ControllerLocatorSingelton;

public class AddSignatureToVerifyActionListener implements ActionListener {
	
	private BasicPanel panel = null;
	JTextField textField;
	File file;
	private static final int PATH_FILE = 3;

	public AddSignatureToVerifyActionListener(JTextField tfApplication,
			BasicPanel basicPanel, File file) {
		this.panel = basicPanel;
		this.textField = tfApplication;
		this.file = file;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (file == null) {
			file = new File("./");
		}

		JFileChooser chooser = new JFileChooser(file);
		chooser.setMultiSelectionEnabled(false);
		chooser.addChoosableFileFilter(new FileFilter() {
			@Override
			public String getDescription() {
				return "*.xml";
			}

			@Override
			public boolean accept(File f) {
				return f.isDirectory()
						|| f.getName().toLowerCase().endsWith(".xml");
			}
		});

		int option = chooser.showOpenDialog(null);
		if (option == JFileChooser.APPROVE_OPTION) {
			File sf = chooser.getSelectedFile();
			textField.setText(sf.getAbsolutePath());

			VerifyPanelDataCVO data = new VerifyPanelDataCVO();
			data.setArchival(panel.getRdbtnFromAs().isSelected());

			FileTableModel model = (FileTableModel) panel.getTableVerify()
					.getModel();
			List<File> files = new ArrayList<File>(model.getRowCount());
			for (int i = 0; i < model.getRowCount(); i++) {
				files.add(new File((String) model.getValueAt(i, PATH_FILE)));
			}
			data.setFiles(files);
			if (data.isArchival()) {
				data.setPath(panel.getTfArchivalPackage().getText());
			} else {
				data.setPath(panel.getTfApplication().getText());
			}

			model.makeEmptyModel();
			ControllerLocatorSingelton application = ControllerLocatorSingelton
					.getInstance();
			List<File> refFiles = application.getSignatureContreoller()
					.getFilesFromSignature(data);
			panel.getConsole().append(
					new Date() + "\t\tSearching reference files:\n");
			for (File file : refFiles) {
				model.addFiles(fillDataFromFileToTileCVO(file));
				panel.getConsole().append(new Date() + "\t\tReference files wass found with reference +"+file.getName()+"\n");
			}
			panel.getConsole().append(
					new Date() + "\t\tSearching reference files finished");
			panel.getConsole().append("\n");
		}
	}

	private FileDetailCVO fillDataFromFileToTileCVO(File file) {
		FileDetailCVO result = new FileDetailCVO();
		String[] name = file.getName().split("\\.");
		if (name.length > 1) {
			result.setName(name[0]);
			result.setExtension(name[name.length - 1]);
		} else {
			result.setName(name[name.length - 1]);
			result.setExtension("");

		}
		result.setPath(file.getAbsolutePath());
		result.setSize(file.length());
		return result;
	}
}
